﻿using UnityEditor;
using UnityEngine;

namespace UPA
{
    public partial class ProcessActionGroupDrawer
    {
        private Vector2 _settingScroll;
        private void RefreshSetting()
        {
           
        }
        private void DrawSetting()
        {
            _settingScroll = EditorGUILayout.BeginScrollView(_settingScroll);
            DrawSettingItems();
            EditorGUILayout.EndScrollView();
            GUILayout.FlexibleSpace();
            DrawAddNewSetting();
            DrawSaveSetting();
        }

        private void DrawSettingItems()
        {
            int removeIndex = -1;
            int copyIndex = -1;
            for (int i = 0; i < _group.Settings.Count; i++)
            {
                EditorGUILayout.BeginHorizontal();
                _group.Settings[i].Name = GUILayout.TextField(_group.Settings[i].Name,_textFieldStyle);

                if (GUILayout.Button("复制", _btnStyle))
                {
                    copyIndex = i;
                }
                
                if (_group.Settings.Count > 1)
                {
                    if (GUILayout.Button("删除", _btnStyle))
                    {
                        removeIndex = i;
                    }
                }
                else
                {
                    _tmpContent.text = "删除";
                    GUILayout.Space(_btnStyle.CalcSize(_tmpContent).x);
                }
                EditorGUILayout.EndHorizontal();
            }

            if (removeIndex >= 0)
            {
                _group.RemoveSetting(removeIndex);
            }

            if (copyIndex >= 0)
            {
                _group.AddNewSetting(JsonUtility.FromJson<ProcessActionArgumentSetting>(JsonUtility.ToJson(_group.Settings[copyIndex])));
            }
        }

        private void DrawAddNewSetting()
        {
            if (GUILayout.Button("新建配置",_btnStyle,GUILayout.ExpandWidth(true)))
            {
                _group.AddNewSetting();
            }
        }

        private void DrawSaveSetting()
        {
            if (GUILayout.Button("保存",_btnStyle,GUILayout.ExpandWidth(true),GUILayout.Height(50)))
            {
                AssetDatabase.SaveAssetIfDirty(serializedObject.targetObject);
            }
        }
    }
}